<?php

namespace App\Services\MakeProductThumbnail;

use Carbon\Carbon;
use App\Models\Product;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Rainsens\Dinglog\Facades\Dinglog;

class MakeProducts
{
	protected function ProductThumbnail()
	{
			
		Product::query()
			->whereNotNull('cover')
			->chunks(20, function (Collection $products) {
				
				if ($products->isEmpty()) return false;

				foreach ($products as $product) {
					DB::transaction(function () use ($product) {
						$url = app(MakeProductThumbnail::class, [
							'product' => $product,
						])->make();

					});

					usleep(100);
					
				}
				
				return true;
				
			});
		
		
	}
	
	public function __invoke()
	{

		Log::debug("========== Start make product thumbnail ......");
		
		$this->ProductThumbnail();
		
		Log::debug("========== end  make product thumbnail.....");
	}
}
